package com.example.day01.presenter;

import android.util.Log;

import com.example.day01.bean.LogBean;
import com.example.day01.bean.RegBean;
import com.example.day01.bean.XiaBean;
import com.example.day01.contract.LoginContract;
import com.example.mvp.presenter.BasePresenter;
import com.example.mvp.view.IView;
import com.example.net.BaseObserable;
import com.example.net.BaseObserver;
import java.util.HashMap;


public class LoginPresenter extends BasePresenter< LoginContract.LoginModel, IView>implements LoginContract.LoginPresenter{

    public LoginContract.LoginView loginView;
    public LoginContract.RegView regView;
    public LoginContract.XiaView xiaView;

    public LoginPresenter(LoginContract.LoginModel mmodel, LoginContract.LoginView loginView) {
        super(mmodel, loginView);
        this.loginView = loginView;
    }

    public LoginPresenter(LoginContract.LoginModel mmodel, LoginContract.RegView regView) {
        super(mmodel, regView);
        this.regView = regView;
    }

    public LoginPresenter(LoginContract.LoginModel mmodel, LoginContract.XiaView xiaView) {
        super(mmodel, xiaView);
        this.xiaView = xiaView;
    }

    @Override
    public void login() {
        HashMap<String, String> map = new HashMap<>();
        map.put("username",loginView.getUsername());
        map.put("password",loginView.getPwd());

        new BaseObserable().doIt(mmodel.requestLogin(map),new BaseObserver<LogBean>(){

            @Override
            public void onNext(LogBean logBean) {
                super.onNext(logBean);
                if(logBean.getErrorCode()==0){
                    Log.e("xxr", "onNext:登录成功 "+logBean );
                    loginView.goMain();
                }else{
                    Log.e("xxr", "onNext:登录失败 ");
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.e("xxr", "onError: "+e.getMessage() );
            }
        });


    }

    @Override
    public void register() {
        HashMap<String, String> map = new HashMap<>();
        map.put("username",regView.getUsername());
        map.put("password",regView.getPwd());
        map.put("repassword",regView.getRePwd());

        new BaseObserable().doIt(mmodel.requestRegister(map),new BaseObserver<RegBean>(){
            @Override
            public void onNext(RegBean regBean) {
                super.onNext(regBean);
                if(regBean.getErrorCode()==0){
                    Log.e("xxr", "onNext: 注册成功"+regBean );
                    regView.goMain();
                }else{
                    Log.e("xxr", "onNext: 注册失败" );
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.e("xxr", "onError: "+e.getMessage() );
            }
        });

    }

    @Override
    public void Xiadata() {
        HashMap<String, String> map = new HashMap<>();
        map.put("stage_id","1");
        map.put("limit","20");
        map.put("page","10");

        new BaseObserable().doIt(mmodel.requestXia(map),new BaseObserver<XiaBean>(){
            @Override
            public void onNext(XiaBean xiaBean) {
                super.onNext(xiaBean);
                if(xiaBean.toString()!=null){
                    xiaView.XiaMed(xiaBean.toString());
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });

    }
}
